<template>
	<view class="add bg_color pdd_bottom2">
		<view v-if="!isEnter">
			<view class="tit">
				<view class="text">违法信息</view>
				<view class="xing">*</view>
			</view>
			<Form ref="formRef" :formList="formList" :formData="formData"></Form>
			<view class="tit">
				<view class="text">举报人信息</view>
				<view class="xing">*</view>
			</view>
			<Form ref="formRefs" :formList="dangShi" :formData="formData"></Form>
			<view class="butt font28" @click="addItem">上报线索</view>
		</view>
		<SubmitState v-else :success="success" @successReturn="fanhui" @failReturn="isEnter = false"></SubmitState>
	</view>
	<FuJinSubmitModal :deleteShow="deleteShow1" @confirm="confirm1" @close="deleteShow1 = false"></FuJinSubmitModal>
</template>
<script>
import changeData from 'utils/changeData.js';
export const formList1 = [
	{
		label: '标题',
		prop: 'title',
		type: 'text',
		placeholder: '请输入标题',
		rules: [
			{
				required: true,
				message: '请输入标题',
				trigger: ['change', 'blur']
			}
		]
	},
	{
		label: '线索类型',
		prop: 'translationClues',
		type: 'popup',
		placeholder: '请选择线索类型',
		rules: [
			{
				required: true,
				message: '请选择线索类型',
				trigger: ['change', 'blur']
			}
		],
		columns: [
			
		]
	},
	{
		label: '发生时间',
		prop: 'illegalTime',
		type: 'datetime',
		placeholder: '请选择时间',
		mode: 'datetime',
		rules: [
			{
				required: true,
				message: '请选择时间',
				trigger: ['change', 'blur']
			}
		],
		format: 'YYYY-MM-DD HH:mm'
	},
	{
		label: '所在地区',
		prop: 'province',
		type: 'picker',
		placeholder: '请选择所在地区',
		rules: [
			{
				required: true,
				message: '请选择所在地区',
				trigger: ['change', 'blur']
			}
		]
	},
	{
		label: '详细地址',
		prop: 'illegalPlace',
		type: 'textarea',
		rules: [
			{
				required: true,
				message: '请输入地址',
				trigger: ['change', 'blur']
			}
		],
		placeholder: '请输入地址'
	},
	{
		label: '具体情况',
		prop: 'illegalContent',
		type: 'textarea',
		rules: [
			{
				required: true,
				message: '请输入内容',
				trigger: ['change', 'blur']
			}
		],
		placeholder: '请输入内容'
	}
];
export const dangShi = [
	{
		label: '姓名',
		prop: 'informantName',
		type: 'text',
		placeholder: '请输入姓名',
		rules: [
			{
				required: true,
				message: '请输入姓名',
				trigger: ['change', 'blur']
			}
		]
	},
	{
		label: '身份证号码',
		prop: 'informantCard',
		type: 'text',
		placeholder: '请输入正确的身份证号码',
		rules: [
			{
				required: true,
				message: '请输入正确的身份证号码',
				validator: changeData.cardRouls,
				trigger: ['change', 'blur']
			}
		]
	},
	{
		label: '性别',
		prop: 'informantSex',
		type: 'popup',
		placeholder: '输入身份证自动填入',
		disabled:true,
		rules: [
			{
				required: true,
				message: '输入身份证自动填入',
				trigger: ['change', 'blur'],
				transform(value) {
					return String(value);
				}
			}
		],
		columns: [
			{ name: '男', value: 1 },
			{ name: '女', value: 2 }
		]
	},
	{
		label: '年龄',
		prop: 'informantAge',
		type: 'text',
		placeholder: '输入身份证自动填入',
		disabled:true,
		rules: [
			{
				required: true,
				message: '请输入年龄',
				trigger: ['change', 'blur'],
				transform(value) {
					return String(value);
				}
			}
		]
	},
	{
		label: '电话号码',
		prop: 'informantPhone',
		type: 'text',
		rules: [
			{
				required: true,
				message: '请输入正确的电话号码',
				pattern: changeData.phonePattern,
				trigger: ['change', 'blur']
			}
		]
	},
	{
		label: '家庭住址',
		prop: 'informantAddress',
		type: 'textarea',
		placeholder: '请输入家庭住址',
		rules: [
			{
				required: true,
				message: '请输入家庭住址',
				trigger: ['change', 'blur']
			}
		]
	}
];
</script>
<script setup>
import Form from 'com/Form.vue';
import SubmitState from 'com/modal/SubmitState.vue';
import FuJinSubmitModal from 'com/modal/FuJinSubmitModal.vue';
import Api from 'api';
import { usersStore } from 'store/users.js';
import dayjs from 'dayjs';
import { dictStore } from 'store/dicts.js';
const store1 = dictStore();
const api = new Api
const store = usersStore();
const id = ref('');
const formRef = ref(null);
const formRefs = ref(null);
const isEnter = ref(false);
const deleteShow1 = ref(false);
const success = ref('');
const formList = ref([...formList1]);
onLoad(async (query) => {
	const res = await store1.getDict('crime');
	formList.value[1].columns = res;
	if (query.id) {
		uni.setNavigationBarTitle({
			title: '编辑违法犯罪线索'
		});
		id.value = query.id;
		const res = await api.queryById('legalActivity', { id: query.id });
		formData.value = res;
	} else {
		uni.setNavigationBarTitle({
			title: '上报违法犯罪线索'
		});
	}
});
const formData = ref({
	title: '',
	translationClues: '',
	illegalTime: '',
	province: '',
	illegalPlace: '',
	illegalContent: '',
	informantName: store.users.username,
	informantAge: '',
	informantSex: '',
	informantPhone: store.users.phone,
	informantCard: '',
	informantAddress: ''
});
watch(
	() => formData.value.informantCard,
	(newVal) => {
		const flag = changeData.cardRouls(null, newVal);
		if (flag) {
			const birthYear = newVal.substring(6, 10);
			const birthMonth = newVal.substring(10, 12);
			const birthDay = newVal.substring(12, 14);
			const birthDate = `${birthYear}-${birthMonth}-${birthDay}`;
			const today = dayjs();
			const birth = dayjs(birthDate);
			const age = today.diff(birth, 'year');
			const gender = newVal[16] % 2 == 0 ? 2 : 1;
			formData.value.informantAge = age
			formData.value.informantSex = gender
		}
	}
);
const fanhui = () => {
	uni.navigateBack()
};
const addItem = () => {
	const mise = new Promise((resolve) => {
		formRef.value.submit(() => {
			resolve();
		});
	});
	const mise1 = new Promise((resolve) => {
		formRefs.value.submit(() => {
			resolve();
		});
	});
	let submitPromises = [mise, mise1];
	Promise.all(submitPromises).then(() => {
		store.isFuJing() ? (deleteShow1.value = true) : end();
	});
};
const confirm1 = () => {
	end();
};
const end = async () => {
	deleteShow1.value = false;
	let res;
	if (id.value) {
		res = await api.edit('legalActivity',unref(formData));
	} else {
		res = await api.add('legalActivity', unref(formData));
	}
	success.value = res;
	isEnter.value = true;
};
</script>

<style lang="scss" scoped></style>
